package com.example.demo.service.impl;

import com.example.demo.constant.RedisKeyConstant;
import com.example.demo.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;

import java.util.Map;

/**
 * @author Code Farmer
 * @date 2020/5/8 16:17
 */
public class CartServiceImpl implements CartService {

    @Autowired
    private HashOperations<String, String, Object> hashOperations;

    @Override
    public void addCart(String userId, String goodsId) {
        hashOperations.put(String.format(RedisKeyConstant.USER_CART, userId), goodsId, 1);
    }

    @Override
    public long addCartOne(String userId, String goodsId) {
        return hashOperations.increment(String.format(RedisKeyConstant.USER_CART, userId), goodsId, 1);
    }

    @Override
    public long getCartCount(String userId) {
        return hashOperations.size(String.format(RedisKeyConstant.USER_CART, userId));
    }

    @Override
    public long delCartGoods(String userId, String goodsId) {
        return hashOperations.delete(String.format(RedisKeyConstant.USER_CART, userId), goodsId);
    }

    @Override
    public Map<String, Object> getAllCartGoods(String userId) {
        Map<String, Object> allGoods = hashOperations.entries(String.format(RedisKeyConstant.USER_CART, userId));
        return allGoods;
    }

}
